package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.repository.DestinationMongoRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @RequestMapping("list")
    public String list(Model model , @ModelAttribute("qo") QueryObject qo){
        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page" , page);
        return "strategy/list";
    }

    @RequestMapping("listDests")
    @ResponseBody
    public Object listDests(){
        return destinationService.list();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Strategy strategy){
        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }

    @RequestMapping("input")
    public String input(Model model ,String id){

        List<StrategyTheme> themes = strategyThemeService.list();
        model.addAttribute("themes" ,themes);
        // 通过当前id ,查询当前对象
        if(StringUtils.hasLength(id)){
            Strategy strategy = strategyService.get(id);
            // 把对象共享给前端用于回显
            model.addAttribute("strategy" , strategy);
        }
        // 判断只要有id就执行编辑方法
        if(StringUtils.hasLength(id)){
        }
        model.addAttribute("catalogs" ,strategyCatalogService.queryGroupList());
        return "strategy/input";
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id){
        if(StringUtils.hasLength(id)){
            strategyService.delete(id);
        }
        return JsonResult.success();
    }

}
